<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Relations\HasOne;

/**
 */
class MomentsProfile extends Model
{
    public $timestamps = false;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_moments_profiles';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * 关联文章图片
     * @return HasOne
     */
    public function with_fileinfo()
    {
        return $this->hasOne(Fileinfo::class, 'fileinfo_id', 'fileinfo_id')->select(['fileinfo_id', 'url']);
    }

    /**
     * 关联商品
     * @return HasOne
     */
    public function with_products()
    {
        return $this->hasOne(Product::class, 'id', 'product_id')
            ->with(['shop_min_sku'])
            ->select(['product_name', 'id'])
            ->where(function ($where) {
                $where->whereNull('deleted_at')
                    ->whereHas('industry_info', function ($with) {
                        $with->where('is_unreal', '=', 1);
                    })
                    ->where(['is_illegal' => 0, 'is_show' => 1, 'check_status' => 1]);
            })
            ->orWhere(function ($where) {
                $where->where('delivery_id', '>', 0)
                    ->where(['is_illegal' => 0, 'is_show' => 1, 'check_status' => 1]);
            });
    }

    /**
     * 获取有效推广商品配置信息
     * @return HasOne
     */
    public function with_valid_shop_ad()
    {
        return $this->hasOne(FlyShopAd::class, 'ad_id', 'id')
            ->where('is_show', '=', 1)
            ->whereNull('deleted_at');
    }
}